package net.skyedu.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.skyedu.bean.StuFeeDetail;
import net.skyedu.dao.AjaxDAO;

public class ActualFundList extends HttpServlet{
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");

		
		int courseid = Integer.parseInt(request.getParameter("courseid"));
		int studentId = Integer.parseInt(request.getParameter("stuId"));
		int SubjectId = Integer.parseInt(request.getParameter("subId"));
		int GradeId = Integer.parseInt(request.getParameter("graId"));
		int PeriodId = Integer.parseInt(request.getParameter("terId"));
		
		int currentpage = Integer.parseInt(request.getParameter("currentPage"));
  		int pagesize = Integer.parseInt(request.getParameter("pageSize"));
  		
		//JSONArray json = ajaxDAO.ActualFee(studentId, SubjectId, GradeId, PeriodId, startIndex, pagesize);
		/*
		PrintWriter out = response.getWriter();
	    out.print(json);
	    out.flush();
	    out.close();
	    */
	    
	    
	    int startIndex = 0;
	    StringBuffer jsonAttr = new StringBuffer("[");
//	    JSONArray json = new JSONArray();
	    List<StuFeeDetail> stufeeList=new ArrayList<StuFeeDetail>();
  		try{
    	  startIndex = (currentpage - 1) * pagesize;
    	  AjaxDAO ajaxDAO = new AjaxDAO();
	      int pageCount = ajaxDAO.ActualRefundFee(studentId, SubjectId, GradeId, PeriodId, startIndex, pagesize,0,courseid).size();
	      if(pageCount != 0){
	    	  stufeeList = ajaxDAO.ActualRefundFee(studentId, SubjectId, GradeId, PeriodId, startIndex, pagesize,1,courseid);
//	    	  "<td>"+json[i].SubjectName+"</td>"+
//	          "<td>"+json[i].DeptName+"</td>"+
//	          "<td>"+json[i].FeeTime+"</td>"+
//	          "<td>"+json[i].AlreadyFee+"</td>"+
	    	  for(int i = 0; i < stufeeList.size(); i++){
		    	  StuFeeDetail att = stufeeList.get(i);
		    	  jsonAttr.append("{");
		    	  jsonAttr.append("\"stuName\":\""+att.getStudentName()+"\",");
		    	  jsonAttr.append("\"SubjectName\":\""+att.getSubjectName()+"\",");
		    	  jsonAttr.append("\"DeptName\":\""+att.getDeptName()+"\",");
		    	  if("".equals(att.getFeeTime())||null==att.getFeeTime())
		    	  {
		    		  jsonAttr.append("\"FeeTime\":\""+"--"+"\",");
		    	  }
		    	  else
		    	  {
		    		  String time=att.getFeeTime();
		    		  time=time.substring(0, 10);
		    	  jsonAttr.append("\"FeeTime\":\""+time+"\",");
		    	  }
		    	  jsonAttr.append("\"AlreadyFee\":\""+att.getActualCharged()+"\",");		    	  
		    	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); //记录总数
		    	  jsonAttr.append("}");
		    	  if(i<stufeeList.size()-1){
		    		  jsonAttr.append(",");
		    	  }
		       }
	    	  
		      
	      }else{
	    	  jsonAttr.append("{");
	    	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); 
	    	  jsonAttr.append("}");
	      }
	      jsonAttr.append("]");
	    PrintWriter out = response.getWriter();
	    out.print(jsonAttr);
	    System.out.println(jsonAttr);
	    out.flush();
	    out.close();
	    
      }catch(Exception e){
    	  //写到日志表中
    	  e.printStackTrace();
      }
		
	}
}
